<?php

  class LoginAction extends Action{
  
    public function login(){
	
	  if(!empty($_POST)){
	  
	    //dump(session('verify'));
	    
		if(md5(I('verify'))!=session('verify')){
		
		  $ajax=3;
		  
		  $this->ajaxReturn($ajax);
		
		}
	  
	  
	    $re=M('dailiuser')->where('username="%s" and password="%s" and type=1',I('username'),md5(I('password')))->find();
		
		if($re!=null){
		
		  session('dailiname',$re['username']);
		  session('did',$re['id']);
		  session('isdlogin','1');
		  $ajax=1;
		
		}else{
		
		  $ajax=2;
		
		}
	    
	    $this->ajaxReturn($ajax);
	  
	  }
	
	 
	  layout(false);
	  $this->display();
	
	}
	
	
	public function savepass(){
	  
	  if(session('did')==""){
	  
	    $this->redirect('Login/login');
	  
	  }
	  
	  
	  if(!empty($_POST)){
		  if(I('password')!=I('repass')){
		  
			$this->error('两次密码输入不一致');
		  
		  }
		  
		  $arr['password']=md5(I('password'));
		  
		  $re=M('dailiuser')->where('id="%s"',session('did'))->save($arr);
		 
		  if($re!==false){
		  
		    $this->success('修改成功');exit();
		  
		  }else{
		  
		    $this->error('修改失败');
		  
		  }
		  
	  }
	  
	  
	  
	
	
	  $this->display();
	
	}
	
	
	public function reg(){
	
	  if(!empty($_POST)){
	  
	    if(md5(I('verify'))!=session('verify')){
		
		  $ajax=4;//验证码错误
		  
		  $this->ajaxReturn($ajax);
		
		}
		
		
		if(I('password')=='' or I('tixianpwd')==""){
		  
		  $ajax=6;//提现密码或者注册密码不能为空
		  
		  $this->ajaxReturn($ajax);
		  
		}
		
		if(I('username')==""){
		  
		   $ajax=7;//用户名不能为空
		  
		   $this->ajaxReturn($ajax);
		  
		
		}
		
		
		if(I('password')!=I('repass')){
		
		  $ajax=5;//两次密码不一样
		  
		  $this->ajaxReturn($ajax);
		
		}
		
		$yaoqingma=M('yaoqingma')->where('yaoqingma="%s" and status="0"',$_POST['yaoqingma'])->find();
		
	
		
		//dump($yaoqingma);
		
		if($yaoqingma==null){
		 
		 $ajax=8;//邀请码错误
		 
		 $this->ajaxReturn($ajax);
		
		
		}
		
		
	  
	    $re=M('dailiuser')->where('username="%s"',I('username'))->find();
		
		if($re!=null){
		  $ajax=3;
		  
		  $this->ajaxReturn($ajax);
		
		}
		
		
		$str='1234567890abcdefghigklmnopqrstuvwxyz';
		$str=str_shuffle($str);
		$str=substr($str,0,6);
		$arr['keyword']=$str.time();
		
		$arr['type']=1;
		$arr['regtime']=time();
		$arr['username']=I('username');
		$arr['password']=md5(I('password'));
	    $arr['tixianpwd']=md5(I('tixianpwd'));
		$arr['yaoqingma']=I('yaoqingma');
		$arr['upid']=$yaoqingma['uid'];
		
		// $arr['upid']=I('upid');
		
		
		
	//	dump($arr);die;
		
		$re=M('dailiuser')->add($arr);
		
		if($re){
		
		  session('did',$re);
		  session('username',I('username'));
		  session('isdlogin','1');
		  
		  $status['status']=1;
		  
		  M('yaoqingma')->where('yaoqingma="%s"',I('yaoqingma'))->save($status);
		  
		  
		  $ajax=1;
		
		}else{
		
		  $ajax=2;
		
		}
		
		$this->ajaxReturn($ajax);
	
	  
	  }
	  
	  
	  if(isset($_GET['id'])){
	  
	    $this->id=$_GET['id'];
	  
	  }
	  
	
	  layout(false);
	
	  $this->display();
	
	}
	
	  public function verify(){
			import('ORG.Util.Image');
			ob_clean();
			Image::buildImageVerify();
		}
		
    public function savezhifupwd(){
	
	   if(session('did')==""){
	  
	    $this->redirect('Login/login');
	  
	  }
	  
	  
	  if(!empty($_POST)){
	  
	      $my=M('dailiuser')->where('id="%s"',session('did'))->find();
		  
		  if(md5($_POST['oldpass'])!=$my['zhifupwd']){
		  
		    $this->error('原支付密码错误');
		  
		  }
	      
		   if(I('password')==""){
		  
		    $this->error('新密码不能为空');
		  
		  }
	  
		  if(I('password')!=I('repass')){
		  
			$this->error('两次密码输入不一致');
		  
		  }
		  
		  $arr['zhifupwd']=md5(I('password'));
		  
		  $re=M('dailiuser')->where('id="%s"',session('did'))->save($arr);
		 
		  if($re!==false){
		  
		    $this->success('修改成功');exit();
		  
		  }else{
		  
		    $this->error('修改失败');
		  
		  }
		  
	  }
	  
	  $this->display();
	
	
	}
	
	
	
	public function outlogin(){
	
	  session(null);
	
	  $this->success('退出成功');
	
	}		
		
  
  
  }